package com.sunshuo.springsecuritydemo.util;

import com.sunshuo.springsecuritydemo.pojo.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @ClassName AuthUtils
 * @Description TODO
 * @Author sunshuo
 */
public class AuthUtils {

    public static User getPrincipal() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            return null;
        }
        final Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            return (User) principal;
        }
        return null;
    }


}
